<?php
namespace app\admin\controller;
use think\Db;
class Com extends Base
{
    //通用编辑
    public function edit(){
        if(is_post()){
            $d = input();
            if(!in_array($d['field'], ['sort'])){
                return err('非通用字段');
            }
            Db::name($d['table'])->where("id", $d['id'])->update([$d['field']=>$d['value']]);
            session('js.function','reload');
            return suc();
        }
    }

    //通用 拷贝一条数据
    function copys(){
        if(is_post()){
            $table = input('table');
            $id = (int)input('id');
            $temp = [
                "slide",
                "link",
                "goods",
                "article",
            ];
            $info = Db::name($table)->find($id);
            if(!in_array($table, $temp)){
                return err('不可复制');
            }elseif($info){
                unset($info['id']);
                $info['create_time'] = time();
                $info['update_time'] = time();
                $info['title'] .= ' 拷贝';
                Db::name($table)->insert($info);
                session('js.function','reload');
                return suc();
            }
            return err('无数据');
        }
    }
}
